﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Runtime.Serialization.Json;


    public class Common
    {
        public static XmlDocument doc;
        public static XmlNodeList nodeList;
        static FileInfo xmlfile = new FileInfo(System.Windows.Forms.Application.StartupPath + @"/config.xml");
        static Common(){

        }
        public static void setXmlDoc(string rootStr)
        {
            
            doc = new XmlDocument();
            doc.Load(xmlfile.FullName);
            nodeList = doc.SelectSingleNode(rootStr).ChildNodes;
        }

        public static bool saveSystem(string username, string userpwd, string account, string key)
        {
            try
            {
                setXmlDoc("root//System");
                foreach (XmlNode node in nodeList)
                {
                    XmlElement xe = (XmlElement)node;
                    if (xe.Name == "UserName")
                    {
                        xe.SetAttribute("value", username);
                    }
                    if (xe.Name == "UserPwd")
                    {
                        xe.SetAttribute("value", userpwd);
                    }
                    if (xe.Name == "Account")
                    {
                        xe.SetAttribute("value", account);
                    }
                    if (xe.Name == "Key")
                    {
                        xe.SetAttribute("value", key);
                    }
                }
                doc.Save(xmlfile.FullName);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public static bool saveDomain(string domain)
        {
            try
            {
                setXmlDoc("root//System");
                foreach (XmlNode node in nodeList)
                {
                    XmlElement xe = (XmlElement)node;
                    if (xe.Name == "Domain")
                    {
                        xe.SetAttribute("value", domain);
                    }
                    
                }
                doc.Save(xmlfile.FullName);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public static bool saveAPI(string api)
        {
            try
            {
                setXmlDoc("root//System");
                foreach (XmlNode node in nodeList)
                {
                    XmlElement xe = (XmlElement)node;
                    if (xe.Name == "API")
                    {
                        xe.SetAttribute("value", api);
                    }

                }
                doc.Save(xmlfile.FullName);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public static bool saveUserToken(string openid)
        {
            try
            {
                setXmlDoc("root//System");
                foreach (XmlNode node in nodeList)
                {
                    XmlElement xe = (XmlElement)node;
                    if (xe.Name == "Token")
                    {
                        xe.SetAttribute("value", openid);
                    }

                }
                doc.Save(xmlfile.FullName);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public static string GetConfig(string name)
        {
            try
            {
                string val="";
                Common.setXmlDoc("root//System");
                foreach (XmlNode node in Common.nodeList)
                {
                    XmlElement xe = (XmlElement)node;
                    if (xe.Name == name)
                    {
                        val = xe.GetAttribute("value");
                        break;
                    }
                }
                return val;

            }
            catch
            {
                return "";
            }
        }
        public static string MakeSign(string action, long timestamp)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("account={0}&act={1}&key={2}&timestamp={3}", Common.GetConfig("Account"), action, Common.GetConfig("Key"), timestamp);
                return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sb.ToString(), "MD5").ToLower();
            }
            catch (Exception ex)
            {
                return "";
            }
        }

        public static long GetTimestamp()
        {
            TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1);
            return (long)ts.TotalMilliseconds;
        }
        
    }

